package pl.edu.agh.xls2html.htmlelements;

import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.formula.FormulaParser;

import pl.edu.agh.xls2html.js.JSGenerator;

public class Cell implements HTMLelement {

	private HSSFCell cell;
	private HSSFWorkbook wb;
	private Document doc;

	public Cell(HSSFCell c, HSSFWorkbook wb, Document doc) {
		this.doc = doc;
		this.wb = wb;
		this.cell = c;
	}

	private String getCoordinates() {
		int column = cell.getColumnIndex();
		int row = cell.getRowIndex() + 1;
		// TODO zrobic aby po Z robilo AA AB itd
		char chrCol = (char) ('A' + column);
		return chrCol + Integer.toString(row);
	}

	private String makeRTF(String content, HSSFCellStyle style) {
		return content;
	}

	private String getstaticHTML() {
		String content = "";
		HSSFCellStyle style = cell.getCellStyle();

		switch (cell.getCellType()) {
		case HSSFCell.CELL_TYPE_BLANK:
			content = HTMLHelper.NBSP_ELEMENT;
			break;
		case HSSFCell.CELL_TYPE_NUMERIC:
			// DATE
			if (style.getDataFormat() != 0)
				content = cell.getDateCellValue().toString();
			else
				content = Double.toString(cell.getNumericCellValue());
			break;
		case HSSFCell.CELL_TYPE_STRING:
			content = makeRTF(cell.getRichStringCellValue().getString(), style);
			break;
		case HSSFCell.CELL_TYPE_FORMULA:
			HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
			String evaluateCellValue = Double.toString(fe.evaluate(cell)
					.getNumberValue());
			content = makeRTF(evaluateCellValue, style);
			break;
		}

		return HTMLHelper.makeDiv(content);
	}

	private String getDynamicHTML() {
		String content = "";
		HSSFCellStyle style = cell.getCellStyle();
		JSGenerator jsGenerator;

		switch (cell.getCellType()) {
		case HSSFCell.CELL_TYPE_BLANK:
			content = HTMLHelper.makeInput("", getCoordinates());
			break;
		case HSSFCell.CELL_TYPE_NUMERIC:
			// DATE
			if (style.getDataFormat() != 0)
				content = cell.getDateCellValue().toString();
			else
				content = Double.toString(cell.getNumericCellValue());

			content = HTMLHelper.makeInputForFormula(content, getCoordinates());
			break;
		case HSSFCell.CELL_TYPE_STRING:
			content = HTMLHelper.makeInput(cell.getRichStringCellValue()
					.getString(), getCoordinates());
			break;
		case HSSFCell.CELL_TYPE_FORMULA:
			HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
			String evaluateCellValue = Double.toString(fe.evaluate(cell)
					.getNumberValue());

			HSSFEvaluationWorkbook ewb = HSSFEvaluationWorkbook.create(wb);

			content = cell.getCellFormula();
			Ptg[] tokens = FormulaParser.parse(content, ewb);

			jsGenerator = new JSGenerator(tokens);
			content = jsGenerator.getJSFunction();
			String hiddenContent = HTMLHelper.getJSValue(getCoordinates())
					+ "=" + content;

			content = HTMLHelper.makeInputHidden(hiddenContent,
					getCoordinates())
					+ HTMLHelper.makeInputReadOnly(evaluateCellValue,
							getCoordinates());

			break;
		}

		return content;
	}

	@Override
	public String getHTML() {
		String content = "";
		if (cell == null)
			return HTMLHelper.makeCell("", "", 0, 0);

		if (doc.isStaticMode())
			content = getstaticHTML();
		else
			content = getDynamicHTML();

		String cellTag = HTMLHelper.makeCell(content, getCoordinates(), 0, 0);

		if (doc.isStaticMode()) {
			CellProperties prop = new CellProperties(cell);
			if (prop.isInRange())
				if (prop.isFirstInRange())
					cellTag = HTMLHelper.makeCell(content, getCoordinates(),
							prop.getRowspan(), prop.getColspan());
				else
					cellTag = "";
		}
		return cellTag;
	}

	private String getStaticCSS() {
		String css = "";
		CellProperties prop = new CellProperties(cell);
		if (!prop.isInRange())
			css = HTMLHelper.getCSSForStyle(new CellStyle(cell, wb),
					getCoordinates(), doc.isStaticMode());
		if (prop.isInRange() && prop.isFirstInRange()) {
			HSSFSheet sh = cell.getSheet();
			CellRangeAddress cra = prop.getRange();
			HSSFCell lt = sh.getRow(cra.getFirstRow()).getCell(
					cra.getFirstColumn());
			HSSFCell rb = sh.getRow(cra.getLastRow()).getCell(
					cra.getLastColumn());

			CellStyle ltcs = new CellStyle(lt, wb);
			CellStyle rbcs = new CellStyle(rb, wb);

			CellStyle composition = new CellStyle(ltcs.getLeftBorderStyle(),
					ltcs.getTopBorderStyle(), rbcs.getRightBorderStyle(), rbcs
							.getBottomBorderStyle(), ltcs.getBackgroundColor(),
					ltcs.getTextAlign(), prop.getRangeWidth(), prop
							.getRangeHeigt());

			css = HTMLHelper.getCSSForStyle(composition, getCoordinates(), doc
					.isStaticMode());
		}
		return css;
	}

	@Override
	public String getCSS() {
		String css = "";
		if (cell == null)
			return css;

		if (doc.isStaticMode())
			css = getStaticCSS();
		else
			css = HTMLHelper.getCSSForStyle(new CellStyle(cell, wb),
					getCoordinates(), doc.isStaticMode());

		return css;
	}

}
